<?php

namespace common\models\keyword;

use Yii;

/**
 * This is the model class for table "{{%keyword_library}}".
 *
 * @property int $id
 * @property string $text 关键字
 * @property string $pinyin 关键字拼音
 * @property int $length 关键字长度
 * @property int $times 被检索次数
 * @property int $spread_at 推荐时间
 * @property int $created_at
 * @property int $updated_at
 */
class KeywordLibrary extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%keyword_library}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['text'], 'required'],
            [['length', 'times', 'spread_at', 'created_at', 'updated_at'], 'integer'],
            [['text'], 'string', 'max' => 100],
            [['pinyin'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'text' => '关键字',
            'pinyin' => '关键字拼音',
            'length' => '关键字长度',
            'times' => '被检索次数',
            'spread_at' => '推荐时间',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getColor() {
        $tid = 0;
        if ($this->times >= 100 && $this->times < 199) {
            $tid = 1;
        } elseif ($this->times >= 200 && $this->times < 399) {
            $tid = 2;
        } elseif ($this->times >= 400 && $this->times < 799) {
            $tid = 3;
        } elseif ($this->times >= 800) {
            $tid = 4;
        }
        return static::themes($tid);
    }

    public static function themes($level = null) {
        $themes = [
            [
                'hex' => '#ADADAD',
                'rgb' => [173, 173, 173]
            ],
            [
                'hex' => '#FBA0A7',
                'rgb' => [251, 160, 167]
            ],
            [
                'hex' => '#8CDADE',
                'rgb' => [140, 218, 222]
            ],
            [
                'hex' => '#FAC38A',
                'rgb' => [250, 195, 138]
            ],
            [
                'hex' => '#BBD190',
                'rgb' => [187, 209, 144]
            ],
        ];
        if (is_int($level) && isset($themes[$level])) {
            return $themes[$level];
        } else {
            return $themes[0];
        }

        return $themes;
    }

}
